package com.iceGame;

import java.util.ArrayList;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class LevelSelectionScreen implements Screen{
	
	final int NumberOfLevels = 16;
	OrthographicCamera guiCam;
	SpriteBatch batcher;
	Game game;
	Vector3 touchPoint;
	Rectangle[] LevelsBounds;
	Rectangle BackButtonBounds;
	
	public LevelSelectionScreen(Game game){
		this.game=game;
		int width = 800;
		int height = 480;
		batcher = new SpriteBatch();
		guiCam = new OrthographicCamera(width,height);		
		guiCam.position.set(width / 2, height / 2, 0);
		touchPoint = new Vector3();	
		LevelsBounds = new Rectangle[NumberOfLevels];
		BackButtonBounds = new Rectangle(0,0,64,64);
		//init the levels boundaries
		int initX = 31,Xstep = 66, initY = 318, Ystep = 86;
		int levelCounter=1;
		for(int y=0; y < 2; y++){
			for(int x=0; x < 8; x++){
				Rectangle tRec = new Rectangle(initX+(Xstep+30)*x , (initY-(Ystep+30)*y)-94, Xstep, Ystep);
				LevelsBounds[levelCounter-1] = tRec;				
				levelCounter++;
			}						
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float deltaTime) {
		update(deltaTime);
		GLCommon gl = Gdx.gl;
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);	
		batcher.enableBlending();
		batcher.begin();
		batcher.draw(Assets.LevelSelectionScreen, 0, 0, 800, 480);
		//draw locks 
		for(int i=0; i < NumberOfLevels ; i++){
			if(Settings.isLock.get(i) == true){
				Rectangle levelRec = LevelsBounds[i];
				batcher.draw(Assets.lock, levelRec.x+4, levelRec.y+32, 58, 58);
				
			}
			else{
				Rectangle levelRec = LevelsBounds[i];
				for(int k=0; k<Settings.scores.get(i); k++){
					batcher.draw(Assets.smallScoreStar, levelRec.x+5+19*k, levelRec.y+11, 17, 17);
				}
			}
		}		
		batcher.draw(Assets.backButton, BackButtonBounds.x, BackButtonBounds.y, BackButtonBounds.width ,BackButtonBounds.height);
		batcher.disableBlending();
		batcher.end();		
	}
	
	public void update(float deltaTime){
		if (Gdx.input.justTouched()) {
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			
			if (OverlapTester.pointInRectangle(BackButtonBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				game.setScreen(new MainMenuScreen(game));
				this.dispose();
			}
			
			for(int i=0; i<LevelsBounds.length; i++){
				if (OverlapTester.pointInRectangle(LevelsBounds[i], touchPoint.x, touchPoint.y)) {
					if (Settings.soundEnabled)
						Assets.buttonPressSound.play();
					if(Settings.isLock.get(i) == false){
						game.setScreen(new GameScreen(game, i+1));
						this.dispose();
					}
				}	
			}
		}
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

}